import { Controller, Get, Post, Body } from '@nestjs/common';
import { PollingService } from './polling.service';

@Controller('polling')
export class PollingController {
  constructor(private readonly pollingService: PollingService) {}

  @Get()
  getCount() {
    const data = this.pollingService.getCount();
    return {
      code: 200,
      data: {
        message: `当前计数: ${data.count}`,
        count: data.count
      }
    };
  }

  @Get('short')
  getShortPollingCount() {
    const data = this.pollingService.getCount();
    return {
      code: 200,
      data: {
        message: `短轮询计数: ${data.count}`,
        count: data.count
      }
    };
  }

  @Get('long')
  async getLongPollingCount() {
    // 模拟长轮询：等待一段时间后返回数据
    await new Promise(resolve => setTimeout(resolve, 2000));
    const data = this.pollingService.getCount();
    return {
      code: 200,
      data: {
        message: `长轮询计数: ${data.count}`,
        count: data.count
      }
    };
  }

  @Post('increment')
  increment() {
    return this.pollingService.increment();
  }

  @Post('reset')
  reset() {
    return this.pollingService.reset();
  }

  @Post('set-interval')
  setInterval(@Body() body: { interval: number }) {
    return this.pollingService.setInterval(body.interval);
  }
}
